//
//  EvaluateTableViewCell.m
//  B2C
//
//  Created by 李小龙 on 2017/8/16.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "EvaluateTableViewCell.h"
#import "LXLImageButton.h"

#define HeartViewBaseTag        12345
#define PhotoBastTag            1000
@interface EvaluateTableViewCell ()<UITextViewDelegate>
{
    EvaluateModel *_model;
}
@property (nonatomic, strong) UIButton *addImageButton;

@end

@implementation EvaluateTableViewCell

+ (instancetype)evaluateCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    NSString *identifier = @"";
    NSInteger index = 0;
    
    if (indexPath.row < count) {
        identifier = @"cellThird";
        index = 3;
    } else {
        if (indexPath.row == count) {
            identifier = @"evaluateCellFirst";
            index = 0;
        } else if (indexPath.row == count+1) {
            identifier = @"evaluateCellSecond";
            index = 1;
        } else if (indexPath.row == count+2) {
            identifier = @"evaluateCellThird";
            index = 2;
        }
    }
    EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EvaluateTableViewCell" owner:self options:nil] objectAtIndex:index];
        if (indexPath.row == count) {
            HeartFiveView *heartView = [[HeartFiveView alloc] initWithFrame:CGRectMake(ScreenWidth - 15 - [HeartFiveView getFiveHeartViewSizeWithPoint:5.0 withType:HeartTypeForNormal].width, 15, [HeartFiveView getFiveHeartViewSizeWithPoint:5.0 withType:HeartTypeForNormal].width, [HeartFiveView getFiveHeartViewSizeWithPoint:5.0 withType:HeartTypeForNormal].height)];
            heartView.canEdit = YES;
            [heartView setTag:indexPath.section+HeartViewBaseTag];
            [heartView setImageViewWithPoint:5 withType:HeartTypeForNormal];
            [cell.contentView addSubview:heartView];
        }
        if (indexPath.row == count+1) {
            CGFloat buttonW = (ScreenWidth-30.0-15.0*2)/3.0;
            cell.addImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [cell.addImageButton setFrame:CGRectMake(0, 0, buttonW, buttonW)];
            [cell.addImageButton setBackgroundImage:[UIImage imageNamed:@"adddd"] forState:UIControlStateNormal];
            [cell.secondShowPictureView addSubview:cell.addImageButton];
        }
    }
    return cell;
}

- (void)updateCellWith:(EvaluateModel *)model indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    _model = model;
    if (indexPath.row < count) {
        [self.thirdImageView sd_setImageWithURL:GetUrl(model.orderson_model.gimg) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
        [self.thirdNameLabel setText:model.orderson_model.gname];
        [self.thirdCountLabel setText:[NSString stringWithFormat:@"x%ld",(long)model.orderson_model.ognumber.integerValue]];
        [self.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%@",model.orderson_model.gprice]];
//        if ([model.orderson_model.gpricetype isEqualToString:@"0"]) {
//            NSString *allPrice;
//            if (TextIsNotNil(model.orderson_model.goldprice)) {
//                allPrice = model.orderson_model.goldprice;
//            } else {
//                double priceValue = model.orderson_model.gprice.doubleValue * model.orderson_model.gshare.doubleValue * 100;
//                allPrice = [NSString stringWithFormat:@"%.2f",round(priceValue)/100];
//            }
//            [self.thirdCountLabel setText:@"x1"];
//            [self.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%@",allPrice]];
//        } else {
//            [self.thirdCountLabel setText:[NSString stringWithFormat:@"x%ld",(long)model.orderson_model.gshare.integerValue]];
//            [self.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",model.orderson_model.gprice.doubleValue]];
//        }
        // 属性
        NSString *specs = @"";
        for (NSInteger i = 0; i < model.orderson_model.otop.count; i++) {
            OtopsModel *otop = model.orderson_model.otop[i];
            if (i == 0) {
                specs = otop.taname;
            } else {
                specs = [NSString stringWithFormat:@"%@;%@",specs,otop.taname];
            }
        }
        [self.thirdSpecsLabel setText:specs];
    } else {
        if (indexPath.row == count) {
            [[self heartViewWithIndexPath:indexPath] setImageViewWithPoint:model.escore.floatValue withType:HeartTypeForNormal];
        } else if (indexPath.row == count+1) {
            self.secondTextView.delegate = self;
            [self.secondTextView setText:model.econtent];
            // 图片
            self.addImageButton.mj_x = 0;
            self.addImageButton.mj_y = 0;
            [self.addImageButton addTarget:self action:@selector(addImageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            for (LXLImageButton *photo in self.secondShowPictureView.subviews) {
                if ([photo isKindOfClass:[LXLImageButton class]]) {
                    [photo removeFromSuperview];
                }
            }
            
//            [self uploadContentHeight];
            for (NSInteger i = 0; i < _model.imgArray.count; i++) {
                UIImage *image = _model.imgArray[i];
                [self updateBottomView:image count:i];
            }
        } else if (indexPath.row == count+2) {
            //[self.thirdAnonymityButton setSelected:model.isanonymous];
        }
    }
}
#pragma mark - Get
- (HeartFiveView *)heartViewWithIndexPath:(NSIndexPath *)indexPath {
    return [self.contentView viewWithTag:indexPath.section+HeartViewBaseTag];
}

#pragma mark - action
- (void)addImageButtonAction:(UIButton *)button {
    if (self.block) {
        self.block(EvaluateTypeAddImage);
    }
}

- (void)updateBottomView:(UIImage *)image count:(NSInteger)count {
    // 更新UI
    LXLImageButton * photo = [[LXLImageButton alloc] initWithFrame:self.addImageButton.frame];
    //    [photo setTag:self.selectImageArr.count+1];
    [photo.imageView setImage:image];
    [photo.deleteButton addTarget:self action:@selector(imageButtonDelete:) forControlEvents:UIControlEventTouchUpInside];
    [self.secondShowPictureView addSubview:photo];
    
    [photo setTag:count+PhotoBastTag];
    
    BOOL max = [self isMaxCount];
    self.addImageButton.mj_x = photo.right + 15.0;
    if (self.addImageButton.right > self.secondShowPictureView.mj_w) {
        self.addImageButton.mj_x = 0;
        self.addImageButton.mj_y = photo.bottom + 15.0;
        if (!max) {
            [self uploadContentHeight];
        }
    }
}
// imageButton上的删除按钮的点击事件
- (void)imageButtonDelete:(id)sender {
    UIButton *delete = (UIButton *)sender;
    LXLImageButton *photo = (LXLImageButton *)delete.superview;
    [_model.imgArray removeObject:photo.imageView.image];
    NSLog(@"%ld",photo.tag);
    if (self.block) {
        self.block(EvaluateTypeDeleteImage);
    }
    
}
#pragma mark - 是否达到添加最大数 9张
- (BOOL)isMaxCount {
    BOOL temp;
    if (_model.imgArray.count >= 9) {
        temp = YES;
    } else {
        temp = NO;
    }
    [self.addImageButton setHidden:temp];
    
    return temp;
}

// 更新界面高度
- (void)uploadContentHeight {
//    self.contentBackHLayout.constant = _addImageButton.bottom;
//    [self.contentView layoutIfNeeded];
}
#pragma mark - IBAction
- (IBAction)anonymityButtonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setSelected:!button.selected];
    if (self.block) {
        self.block(EvaluateTypeAnonymity);
    }
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    _model.econtent = textView.text;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (self.block) {
        self.block(EvaluateTypeTextViewEdit);
    }
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
